from rest_framework.response import Response

class StandResponseMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        print("Middleware called before view")
        response = self.get_response(request)
        response = self.process_response(response)
        print("Middleware called after view")
        return response

    def process_response(self, response):
        response_ = None
        if isinstance(response, Response):
            data = response.data
            try:
                new_data = {
                    'data': data.get('results'),
                    'status': 'success',
                    'message': 'Data added successfully'
                }
                response_ = Response(data=new_data)
            except TypeError:
                # 如果 data 不能转换为列表，可以记录错误或设置默认值
                pass
            response_['Content-Type'] = 'application/json'
            return response_